package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysTenantDB;
import com.ruoyi.system.mapper.admin.SysTenantMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author cc
 * @ClassName: com.ruoyi.system.service.impl
 * @date: 2025/8/6 14:20
 * @Description:
 */
@Service
@Slf4j
public class SysTenantServiceImpl {

    @Resource
    private SysTenantMapper sysTenantMapper;

    @Resource
    private SysRoleServiceImpl sysRoleService;

    public String selectTenantName(Long tenantId) {

        SysTenantDB sysTenantDB = selectSysTenantById(tenantId);
        if (ObjectUtils.isEmpty(sysTenantDB)){
            return null;
        }
        return sysTenantDB.getTenantName();
    }

    public List<SysTenantDB> selectSysTenantList(SysTenantDB sysTenantDB) {
        if (ObjectUtils.isEmpty(sysTenantDB)){
            return sysTenantMapper.selectList(new LambdaUpdateWrapper<SysTenantDB>());
        }
        // 使用租户名称查询
        LambdaQueryWrapper<SysTenantDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != sysTenantDB.getTenantName(), SysTenantDB::getTenantName, sysTenantDB.getTenantName());
        queryWrapper.eq(null != sysTenantDB.getPrimaryName(), SysTenantDB::getPrimaryName, sysTenantDB.getPrimaryName());
        queryWrapper.eq(null != sysTenantDB.getPrimaryPhone(), SysTenantDB::getPrimaryPhone, sysTenantDB.getPrimaryPhone());
        queryWrapper.eq(null != sysTenantDB.getSecondaryName(), SysTenantDB::getSecondaryName, sysTenantDB.getSecondaryName());
        queryWrapper.eq(null != sysTenantDB.getSecondaryPhone(), SysTenantDB::getSecondaryPhone, sysTenantDB.getSecondaryPhone());
        queryWrapper.eq(null != sysTenantDB.getIsActive(), SysTenantDB::getIsActive, sysTenantDB.getIsActive());


        return sysTenantMapper.selectList(queryWrapper);
    }

    public SysTenantDB selectSysTenantById(Long tenantId) {
        SysTenantDB sysTenantDB = sysTenantMapper.selectById(tenantId);
        return sysTenantDB;
    }


    public int insertSysTenant(SysTenantDB sysTenant) {
        return sysTenantMapper.insert(sysTenant);
    }

    public int updateSysTenant(SysTenantDB sysTenant) {
        if (ObjectUtils.isEmpty(sysTenant.getId())){
            throw new RuntimeException("租户id不能为空");
        }
        return sysTenantMapper.update(sysTenant, new LambdaUpdateWrapper<SysTenantDB>().eq(SysTenantDB::getId, sysTenant.getId()));
    }

    public int deleteSysTenantByIds(Long[] ids) {
        if (ObjectUtils.isEmpty(ids)){
            throw new RuntimeException("租户id不能为空");
        }
        // 更改所有的is_active 为 false
        LambdaUpdateWrapper<SysTenantDB> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SysTenantDB::getId, Arrays.asList(ids));
        wrapper.set(SysTenantDB::getIsActive, Boolean.FALSE);
        return sysTenantMapper.update(null, wrapper);
    }

    public SysTenantDB getTenantByPhoneAndName(String phone, String tenantName) {
        LambdaQueryWrapper<SysTenantDB> sysTenantDBLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysTenantDBLambdaQueryWrapper.eq(SysTenantDB::getPrimaryPhone, phone);
        sysTenantDBLambdaQueryWrapper.eq(SysTenantDB::getTenantName, tenantName);

        return sysTenantMapper.selectOne(sysTenantDBLambdaQueryWrapper);
    }
}
